/*
 * @Description: async await 拦截错误
 * @Autor: lcz
 * @Date: 2022-08-17 09:27:40
 * @LastEditors: lcz
 * @LastEditTime: 2022-08-26 11:42:19
 * @address:https://github.com/scopsy/await-to-js/blob/master/src/await-to-js.ts
 */
export function to<T, U = Error>(
  promise: Promise<T>,
  errorExt?: object
): Promise<[null, T] | [U, null]> {
  return promise
    .then<[null, T]>((data: T) => [null, data])
    .catch<[U, null]>((err: U) => {
      if (errorExt) {
        const parsedError = Object.assign({}, err, errorExt);
        return [parsedError, null]
      }
      return [err, null]
    });
}